package com.example.util;

import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

@ExtendWith(MockitoExtension.class)
public class HttpClientUtilTest2 {
    @Test
    public void testCall() throws Exception {
        MockWebServer server = new MockWebServer();

        server.enqueue(new MockResponse().setBody("hello"));
        server.enqueue(new MockResponse().setBody("world"));
        server.enqueue(new MockResponse().setBody("!!!!"));

        server.start();
    }

    @Test
    public void testCall2() throws IOException{
        MockWebServer server = new MockWebServer();

        try {
            server.enqueue(new MockResponse().setBody("hello, world!").setResponseCode(200));

            HttpUrl httpUrl = server.url("/hello");

            // TODO: Rule does not currently work when just using the OkHttpClient default constructor
            OkHttpClient client = new OkHttpClient.Builder()
                    .build();

            Request request = new Request.Builder()
                    .url(httpUrl)
                    .build();

            Response response = client.newCall(request).execute();

            assertEquals(200, response.code());

        } finally {
            server.shutdown();
            server.close();
        }
    }
}

//Generated with love by TestMe :) Please report issues and submit feature requests at: http://weirddev.com/forum#!/testme